package com.inspinia.message.common.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

/**
 * 通知
 * <p>
 * Created by veblen on 2018-1-8.
 */
public class Notice implements Serializable {
    /**
     *
     */
    private Long id;
    /**
     * 标题
     */
    private String title;
    /**
     * 类型（枚举）
     */
    private NoticeType type = NoticeType.normal;
    /**
     * 内容
     */
    private String content;
    /**
     * 关联类型
     */
    private NoticeLinkType linkType;
    /**
     * 关联实体的信息,Json格式
     */
    private String linkData;
    /**
     * 发送时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    private Date sendTime;
    /**
     * 发送人id
     */
    private Long createBy;
    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    private Date createTime;
    /**
     * 有效期开始时间，到达该时间后，通知开始推送。
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    private Date validityPeriodStart;

    /**
     * 有效期结束时间，如果过了有效期，用户即使在线，系统也不会主动发送通知，但是可以在消息列表中查看已过期的通知。
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    private Date validityPeriodEnd;

    /**
     * 接收者类型。冗余字段，不持久化
     */
    private NoticeAddresseeType addresseeType;
    /**
     * 推送记录id
     */
    private Long noticePushId;
    /**
     * 接收者id
     */
    private Long addresseeId;

    /**
     * 推送状态
     */
    private NoticePushStatus noticePushStatus;
    /**
     * 阅读时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    private Date readTime;
    /**
     * 是否播放声音，根据用户设置表赋值，默认true
     */
    private boolean playAudio = true;


    public Notice() {
    }

    public Notice(String title, NoticeType type, String content, NoticeLinkType linkType, String linkData, NoticeAddresseeType addresseeType) {
        this.title = title;
        this.type = type;
        this.content = content;
        this.linkType = linkType;
        this.linkData = linkData;
        this.addresseeType = addresseeType;
    }

    public boolean isPlayAudio() {
        return playAudio;
    }

    public void setPlayAudio(boolean playAudio) {
        this.playAudio = playAudio;
    }

    public NoticePushStatus getNoticePushStatus() {
        return noticePushStatus;
    }

    public void setNoticePushStatus(NoticePushStatus noticePushStatus) {
        this.noticePushStatus = noticePushStatus;
    }

    public Date getReadTime() {
        return readTime;
    }

    public void setReadTime(Date readTime) {
        this.readTime = readTime;
    }

    public Long getAddresseeId() {
        return addresseeId;
    }

    public void setAddresseeId(Long addresseeId) {
        this.addresseeId = addresseeId;
    }

    public Long getNoticePushId() {
        return noticePushId;
    }

    public void setNoticePushId(Long noticePushId) {
        this.noticePushId = noticePushId;
    }

    public NoticeAddresseeType getAddresseeType() {
        return addresseeType;
    }

    public void setAddresseeType(NoticeAddresseeType addresseeType) {
        this.addresseeType = addresseeType;
    }

    public Date getValidityPeriodStart() {
        return validityPeriodStart;
    }

    public void setValidityPeriodStart(Date validityPeriodStart) {
        this.validityPeriodStart = validityPeriodStart;
    }

    public Date getValidityPeriodEnd() {
        return validityPeriodEnd;
    }

    public void setValidityPeriodEnd(Date validityPeriodEnd) {
        this.validityPeriodEnd = validityPeriodEnd;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return this.id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public void setContent(String content) {
        this.content = content;
    }

    public String getContent() {
        return this.content;
    }

    public NoticeType getType() {
        return type;
    }

    public void setType(NoticeType type) {
        this.type = type;
    }

    public NoticeLinkType getLinkType() {
        return linkType;
    }

    public void setLinkType(NoticeLinkType linkType) {
        this.linkType = linkType;
    }

    public String getLinkData() {
        return linkData;
    }

    public void setLinkData(String linkData) {
        this.linkData = linkData;
    }

    public void setSendTime(Date sendTime) {
        this.sendTime = sendTime;
    }

    public Date getSendTime() {
        return this.sendTime;
    }

    public void setCreateBy(Long createBy) {
        this.createBy = createBy;
    }

    public Long getCreateBy() {
        return this.createBy;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getCreateTime() {
        return this.createTime;
    }

    public boolean validityPeriodNotSet() {
        return validityPeriodStart == null && validityPeriodEnd == null;
    }

    /**
     * 通知是否在有效期内
     *
     * @return
     */
    public boolean inValidityPeriod() {
        Date now = new Date();
        if (validityPeriodStart != null && (validityPeriodStart.compareTo(now) <= 0)
                || !isExpired()) {
            return true;
        }
        return false;
    }

    /**
     * 通知是否过期
     *
     * @return
     */
    public boolean isExpired() {
        return validityPeriodEnd != null && (validityPeriodEnd.compareTo(new Date()) <= 0);
    }

    /**
     * 是否可以推送
     * @return
     */
    public boolean pushable() {
        return validityPeriodNotSet() || inValidityPeriod();
    }
}